﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRM.Model
{
    public class EducationService : IEntityService<TrinhDoHocVan>
    {
        public List<TrinhDoHocVan> GetAll()
        {
            using (HRMEntities entities = new HRMEntities())
            {
                return entities.TrinhDoHocVans.ToList();
            }
        }

        public bool Add(TrinhDoHocVan pb)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                entities.TrinhDoHocVans.Add(pb);
                entities.SaveChanges();
                return true;
            }
        }

        public bool Update(TrinhDoHocVan pb)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.TrinhDoHocVans.Where(x => x.MaTrinhDo == pb.MaTrinhDo).FirstOrDefault();
                if (query != null)
                {
                    query.TenTrinhDo = pb.TenTrinhDo;
                    query.ChuyenNganh = pb.ChuyenNganh;
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public bool Delete(TrinhDoHocVan pb)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.TrinhDoHocVans.Where(x => x.MaTrinhDo == pb.MaTrinhDo).FirstOrDefault();
                if (query != null)
                {
                    entities.TrinhDoHocVans.Remove(query);
                    entities.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        public bool DeleteList(List<TrinhDoHocVan> list)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                foreach (var item in list)
                {
                    var query = entities.TrinhDoHocVans.Where(x => x.MaTrinhDo == item.MaTrinhDo).FirstOrDefault();
                    entities.TrinhDoHocVans.Remove(query);
                }
                entities.SaveChanges();
                return true;
            }
        }

        public List<TrinhDoHocVan> GetPaging(int pageIndex, int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                return entities.TrinhDoHocVans.OrderBy(p => p.MaTrinhDo).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        public int GetNumberOfPages(int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                int count = entities.TrinhDoHocVans.Count();
                int pages = count / pageSize;
                if (count > pages * pageSize) return pages + 1;
                return pages;
            }
        }
    }
}
